
package posturecorrectionsystem.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ahmet
 */
public class PostureWarningSystem {

    private static boolean isWindows() {
        return (System.getProperty("os.name").toLowerCase().indexOf("windows")!=-1);
    }

    private static boolean isLinux() {
        return (System.getProperty("os.name").toLowerCase().indexOf("linux")!=-1);
    }

    public static void popup(String message) {
        //Here we construct a popup window
        //and show on the computer screen
        final JDialog popupWindow = new JDialog();
        popupWindow.setTitle("Posture Correction Warning");
        popupWindow.setSize(300, 100);
        popupWindow.getContentPane().setLayout(new BorderLayout());

        JLabel warningLabel = new JLabel(message, JLabel.CENTER);

        JPanel jp = new JPanel(new FlowLayout());
        JButton closeButton = new JButton("Close");
        closeButton.setMnemonic('C');
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                popupWindow.dispose();
            }
        });
        jp.add(closeButton);

        popupWindow.getContentPane().add(warningLabel, BorderLayout.CENTER);
        popupWindow.getContentPane().add(jp, BorderLayout.SOUTH);

        popupWindow.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        popupWindow.setAlwaysOnTop(true);
        popupWindow.setVisible(true);
    }

    public static void sound() {
        try {
            AudioPlayer.player.start(new AudioStream(new FileInputStream("ding.wav")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void restriction() {
        //fill the screen with a dialog
    }

    public static void systemBeep(){
        Toolkit.getDefaultToolkit().beep();
    }

    public static void warn(String settingsFileExtension, String settingsFilename) {
        Properties settings = new Properties();
        try {
            settings.load(new FileInputStream(settingsFilename));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            //if there is no settings file, user must create one
            new SettingsManager(settingsFileExtension, settingsFilename).setVisible(true);
        } catch (IOException ex2) {
            ex2.printStackTrace();
        }

        if (settings.getProperty("warnPosturePopup").equals("true")) {
            popup("Please correct your posture!");
        }

        if (settings.getProperty("warnPostureSound").equals("true")) {
            sound();
        }

        if (settings.getProperty("warnPostureRestriction").equals("true")) {
            restriction();
        }
    }
}